<?php


namespace App\Helpers;


use Spatie\QueryBuilder\AllowedFilter;

class QueryBuilder extends \Spatie\QueryBuilder\QueryBuilder {
    public function filters($filters = [], bool $replace = false): self {
        if (!$replace && is_array($filters)) {
            $filters = array_merge([
                AllowedFilter::exact('id'),
                'name',
                'username', 'mobile',
                AllowedFilter::exact('enable_status'),

                AllowedFilter::scope('CreatedAtOfDay'),
                AllowedFilter::scope('CreatedAtBefore'),
                AllowedFilter::scope('CreatedAtAfter'),
                AllowedFilter::scope('CreatedAtBetween'),

                AllowedFilter::scope('UpdatedAtBefore'),
                AllowedFilter::scope('UpdatedAtAfter'),
                AllowedFilter::scope('UpdatedAtBetween'),

                AllowedFilter::scope('SizeBefore'),
                AllowedFilter::scope('SizeAfter'),
                AllowedFilter::scope('SizeBetween'),
            ],
                $filters);
        }
        return parent::allowedFilters($filters);
    }

    public function fields($fields = [], bool $replace = false): self {
        if (!$replace && is_array($fields)) {
            $fields = array_merge([
                'id',
            ],
                $fields);
        }
        return parent::allowedFields($fields);
    }


    public function includes($includes = [], bool $replace = false): self {
        if (!$replace && is_array($includes)) {
            $includes = array_merge([
                'nature',

            ],
                $includes);
        }
        return parent::allowedIncludes($includes);
    }

    public function sorts($sorts = [], bool $replace = false): self {
        if (!$replace && is_array($sorts)) {
            $sorts = array_merge([
                'id',
            ],
                $sorts);
        }
        return parent::allowedSorts($sorts);
    }

    public function page() {
        if (request()->header('all')) {
            return self::get();
        }
        return self::jsonPaginate();
    }
}
